# encoding=utf-8

import pandas as pd
import pymysql

#### 连接mysql数据库取数据 #####
# 建立数据库连接
conn = pymysql.connect(host="127.0.0.1", user="root", passwd="123456", port=3306, db="data", charset="utf8")
# 通过获取到的数据库连接conn下的cursor()方法来创建游标
cursor = conn.cursor()
# 解决乱码问题
cursor.execute("SET NAMES utf8")
# sql语句
sql = "SELECT * FROM crs_customer_info_00 limit 10"
# read_sql 方法返回的数据类型是DataFrame
dataframe = pd.read_sql(sql, con=conn)
print(dataframe)

# 关闭游标
cursor.close()
# 关闭数据库连接
conn.close()

